//
//  WSCustomizeViewController.m
//  GroupFourFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSCustomizeViewController.h"
#import "WSFavourViewController.h"
#import "WSHistoryViewController.h"

@interface WSCustomizeViewController ()<UIScrollViewDelegate>

@property(nonatomic,retain)UIScrollView *bigScrollView;
@property(nonatomic,retain)UIView *sliderView;
@property(nonatomic,retain)WSBaseBtn *favorBtn;
@property(nonatomic,retain)WSBaseBtn *historyBtn;

@end

@implementation WSCustomizeViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    //改变title颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil]];
    self.navigationItem.title = @"定制听";
    self.navigationController.navigationBar.barTintColor = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createButton];
    [self createScrollView];
    
    [self.view bringSubviewToFront:self.messageView];
    
}

- (void)createButton
{
    
    //收藏按钮
    self.favorBtn = [WSBaseBtn buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.favorBtn];
    [self.favorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left);
        make.top.mas_equalTo(self.view.mas_top);
        make.width.mas_equalTo(self.view.frame.size.width / 2);
        make.height.mas_equalTo(@40);
        
    }];
    
    [self.favorBtn setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
    [self.favorBtn setTitle:@"收藏" forState:UIControlStateNormal];
    self.favorBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    
    [self.favorBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];


    //历史按钮
    self.historyBtn = [WSBaseBtn buttonWithType:UIButtonTypeCustom];
    [self.historyBtn setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
    [self.historyBtn setTitle:@"历史" forState:UIControlStateNormal];
    self.historyBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0];
    [self.view addSubview:self.historyBtn];
    [self.historyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.favorBtn.mas_right);
        make.top.mas_equalTo(self.favorBtn);
        make.height.mas_equalTo(self.favorBtn);
        make.width.mas_equalTo(self.favorBtn);
        
    }];
    
    [self.historyBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)buttonAction:(UIButton *)btn
{
    NSInteger i = 0;
    NSLog(@"%ld",i++);
    if (btn == self.favorBtn) {
        [self.bigScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
    }
    else
    {
        [self.bigScrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
    }
}

- (void)createScrollView
{// 导航栏下面的一条黑线
    UIView *lineUp = [[UIView alloc] init];
    lineUp.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1];
    [self.view addSubview:lineUp];
    [lineUp mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.left.mas_equalTo(@0);
        make.top.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
    }];
    
    //外层scrollview
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    self.bigScrollView = [[UIScrollView alloc] init];
    self.bigScrollView.backgroundColor = COLOR_BG;
    [self.view addSubview:self.bigScrollView];
    [self.bigScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.view);
        make.top.mas_equalTo(@40);
        make.width.mas_equalTo(self.view);
        make.height.mas_equalTo((self.view.frame.size.height - 111));
        
    }];
    
    
    
    self.bigScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*2, HEIGHT_TABLE);
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.bounces = NO;
    self.bigScrollView.delegate = self;

    self.sliderView = [[UIView alloc] init];
    self.sliderView.backgroundColor = [UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1];
    [self.view addSubview:self.sliderView];
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@0);
        make.top.equalTo(self.favorBtn.mas_bottom);
        make.width.mas_equalTo(@(SCREEN_WIDTH / 2));
        make.height.mas_equalTo(@2);
        
    }];
    
    WSFavourViewController *favourVC = [[WSFavourViewController alloc] init];
    favourVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self addChildViewController:favourVC];
    [self.bigScrollView addSubview:favourVC.view];
    WSHistoryViewController *historyVC = [[WSHistoryViewController alloc] init];
    [self addChildViewController:historyVC];
    [self.bigScrollView addSubview:historyVC.view];
    historyVC.view.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    self.bigScrollView.showsHorizontalScrollIndicator = NO;

    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (self.bigScrollView.contentOffset.x==0) {
        [self.favorBtn setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
        [self.historyBtn setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
        
        [self.sliderView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(@0);
            make.top.mas_equalTo(@40);
            make.width.mas_equalTo(@(SCREEN_WIDTH / 2));
            make.height.mas_equalTo(@2);
        }];
        
        [UIView animateWithDuration:0.2 animations:^{
            
            [self.view layoutIfNeeded];
        }];
    }
    if (self.bigScrollView.contentOffset.x==SCREEN_WIDTH) {
        [self.favorBtn setTitleColor:COLOR_NORMAL forState:UIControlStateNormal];
        [self.historyBtn setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
        
        [self.sliderView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(@(SCREEN_WIDTH / 2));
            make.top.mas_equalTo(@40);
            make.width.mas_equalTo(@(SCREEN_WIDTH / 2));
            make.height.mas_equalTo(@2);
        }];
        
        [UIView animateWithDuration:0.3 animations:^{
            
            [self.view layoutIfNeeded];
        }];
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
